package com.fishing.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.fishing.entity.CashOutOrderSetState;

import QL.ButlerBankItemSendRequest;
import QL.ButlerBankItemSendResponse;
import QL.ButlerCashOutOrderGetListRequest;
import QL.ButlerCashOutOrderGetListResponse;
import QL.ButlerCashOutOrderInfo;
import QL.ButlerCashOutOrderSetStateRequest;
import QL.ButlerCashOutOrderSetStateResponse;
import QL.ButlerGetRechargeOrderListRequest;
import QL.ButlerGetRechargeOrderListResponse;
import QL.ButlerRealGoodsOrderGetListRequest;
import QL.ButlerRealGoodsOrderGetListResponse;
import QL.ButlerRechargeOfflineConfigGetRequest;
import QL.ButlerRechargeOfflineConfigGetResponse;
import QL.ButlerRechargeOfflineConfigSetRequest;
import QL.ButlerRechargeOfflineConfigSetResponse;
import QL.ButlerRechargeOfflineFinishOrderRequest;
import QL.ButlerRechargeOfflineFinishOrderResponse;
import QL.ButlerRechargeOrderInfo;
import QL.QLClient;

@Controller
public class RechargeController {
	
	private static QLClient  client = new QLClient();
    @RequestMapping("recharge/toEveryday")
    public ModelAndView toEveryday(HttpServletRequest httpServletRequest) {
    	ModelAndView mav = new ModelAndView("recharge/toeveryday");
    	return mav;
    }
    
    //region 跳转充值文本描述页面
    @RequestMapping("recharge/describe")
    public ModelAndView describe(HttpServletRequest httpServletRequest) {
    	ModelAndView mav = new ModelAndView("recharge/describe");
    	return mav;
    }
    //endregion
    
    //region 充值数据展示
    @ResponseBody
    @RequestMapping(value="recharge/list",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    public com.alibaba.fastjson.JSONObject list(HttpServletRequest request,String UserId,String ContentType,String State,String StartTime,String EndTime,String PageIndex,String PageCount,String OrderID) {
    	Date  date1 = new Date();//这是用来刚打开页面默认加载当天时间
		SimpleDateFormat date2 = new SimpleDateFormat ("yyyy-MM-dd");
    	ButlerGetRechargeOrderListRequest req = new  ButlerGetRechargeOrderListRequest();
    	List list = new ArrayList();
    	Map<String,Object> map = new HashMap<String,Object>();
    	long  count = 0;
    	req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
    	if(("").equals(UserId)) {
    		req.UserId = 0;
    	}else {
    		req.UserId = Integer.parseInt(UserId);
    	}
    	req.ContentType = Integer.parseInt(ContentType);
    	req.OrderState = Integer.parseInt(State);
    	if(("").equals(StartTime) || null == StartTime) {
    		req.StartTime = date2.format(date1);
    	}else {
    		req.StartTime = StartTime;
    	}
    	if(("").equals(EndTime) || null == EndTime) {
    		req.EndTime = date2.format(date1.getTime() + 60*60*1000*24);
    	}else {
    		req.EndTime = EndTime;
    	}
    	if (OrderID == null || OrderID == "") 
    		req.FilterOrderNo = "";
    	else
    		req.FilterOrderNo = OrderID;
    	req.PageIndex = Integer.parseInt(PageIndex) - 1;
    	req.PageCount = Integer.parseInt(PageCount);
    	ButlerGetRechargeOrderListResponse rsp = new ButlerGetRechargeOrderListResponse();
    	if (req.ZoneId < 0)
			client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
		client.Execute(req, rsp);
    	for(int i = 0;i < rsp.OrderList.size(); i++) {
    		rsp.OrderList.get(i).OrderAmount = rsp.OrderList.get(i).OrderAmount/100;
    	}
    	map.put("data", rsp.OrderList);
		map.put("count", rsp.TotalCount);
		map.put("message", "");
		map.put("status", "200");
		com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject(map);
    	return json;
    }
    //endregion
    
    //region 充值文本描述
    @ResponseBody
    @RequestMapping(value="rechargedescribe/add",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    public String add(HttpServletRequest request,String UserId,String contents) {
    	ButlerRechargeOfflineConfigSetRequest req = new ButlerRechargeOfflineConfigSetRequest();
    	req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
    	req.ConfigString = contents;
    	ButlerRechargeOfflineConfigSetResponse rsp = new ButlerRechargeOfflineConfigSetResponse();
    	
    	if (req.ZoneId < 0)
			client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
		client.Execute(req, rsp);
		
		if(rsp.ErrMsg == "" || rsp.ErrMsg == null)
			return "success";
		else
			return rsp.ErrMsg;
    }
    //endregion
    
    //region 获得充值文本
    @ResponseBody
    @RequestMapping(value="rechargedescribe/list",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    public Map<String,Object> list(HttpServletRequest request) {
    	ButlerRechargeOfflineConfigGetRequest req = new ButlerRechargeOfflineConfigGetRequest();
    	req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
    	ButlerRechargeOfflineConfigGetResponse rsp = new ButlerRechargeOfflineConfigGetResponse();
    	
    	if (req.ZoneId < 0)
			client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
		client.Execute(req, rsp);
		
		//存储在map之中
		Map<String,Object> map = new HashMap<>();
		JSONObject json = new JSONObject();
		json.put("ConfigString", rsp.ConfigString);
		List list = new ArrayList<>();
		list.add(json);
		map.put("data", list);
		map.put("count", 1);
		map.put("message", "");
		map.put("status", "200");
		if(rsp.ErrMsg == "" || rsp.ErrMsg == null)  {
			return map;
		}
		else {
			map.put("data", "");
			map.put("count", 1);
			return map;
		}
    }
    //endregion

    //region 完成线下充值
    @ResponseBody
    @RequestMapping(value="recharge/offlinefinishOrder",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    public String offlinefinishOrder(HttpServletRequest request,String OrderNo) {
    	ButlerRechargeOfflineFinishOrderRequest req = new ButlerRechargeOfflineFinishOrderRequest();
    	req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
    	req.OrderNo = OrderNo;
    	ButlerRechargeOfflineFinishOrderResponse rsp = new ButlerRechargeOfflineFinishOrderResponse();
    	
    	if (req.ZoneId < 0)
			client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
		client.Execute(req, rsp);
		
		if(rsp.ErrMsg == "" || rsp.ErrMsg == null)
			return "success";
		else
			return rsp.ErrMsg;
    }
    //endregion

    //region 跳转提现页面
    @RequestMapping("recharge/CashOutview")
    public ModelAndView CashOutview(HttpServletRequest httpServletRequest) {
    	ModelAndView mav = new ModelAndView("recharge/CashOutview");
    	return mav;
    }
    //endregion
    
    //region 提现页面
    @ResponseBody
    @RequestMapping(value="charge/CashOut", method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    public JSONObject CashOut(HttpServletRequest request,String OrderID,String UserId,String PageIndex,String PageCount,String Statesend,String startRedeemTime,String endRedeemTime,String startHandleTime,String endHandleTime) {
    	ButlerCashOutOrderGetListRequest req = new ButlerCashOutOrderGetListRequest();
    	ButlerCashOutOrderGetListResponse rsp = new ButlerCashOutOrderGetListResponse();
    	Date  date1 = new Date();
		SimpleDateFormat date2 = new SimpleDateFormat ("yyyy-MM-dd");
		req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
		if(OrderID == "") {
			OrderID = "0";
			req.OrderId = Integer.parseInt(OrderID);
		}
		if(UserId == "") {
			UserId = "0";
			req.OrderId = Integer.parseInt(UserId);
		}
		if ((("").equals(startRedeemTime) && ("").equals(endRedeemTime)) && (("").equals(startHandleTime) && ("").equals(endHandleTime))) {
			startRedeemTime = date2.format(date1);
			endRedeemTime = date2.format(date1.getTime() + 60*60*1000*24);
			startHandleTime = date2.format(date1);
			endHandleTime = date2.format(date1.getTime() + 60*60*1000*24);
			req.IsFilterProcessTime = 0;
			req.IsFilterCreateTime = 0;
		}else if (!(("").equals(startRedeemTime) && ("").equals(endRedeemTime)) && (("").equals(startHandleTime) && ("").equals(endHandleTime))) {
			startHandleTime = "";
			endHandleTime = "";
			req.IsFilterProcessTime = 0;
			req.IsFilterCreateTime = 1;
		}else if ((("").equals(startRedeemTime) && ("").equals(endRedeemTime)) && !(("").equals(startHandleTime) && ("").equals(endHandleTime))) {
			startRedeemTime = "";
			endRedeemTime = "";
			req.IsFilterProcessTime = 1;
			req.IsFilterCreateTime = 0;
		}else {
			req.IsFilterProcessTime = 1;
			req.IsFilterCreateTime = 1;
		}
		req.OrderId = Integer.parseInt(OrderID);
		req.UserId  = Integer.parseInt(UserId);
		req.State = Integer.parseInt(Statesend);
		req.StartCreateTime = startRedeemTime;//开始兑换时间
		req.EndCreateTime = endRedeemTime;
		req.StartProcessTime = startHandleTime;
		req.EndProcessTime = endHandleTime;
		req.PageIndex = Integer.parseInt(PageIndex)-1;
		req.PageCount = Integer.parseInt(PageCount);
		if (req.ZoneId < 0)
			client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
		client.Execute(req, rsp);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		JSONObject json = new JSONObject();
		if (rsp.ErrMsg == null) {
			json.put("data", rsp.InfoList);
			json.put("count", rsp.TotalCount);
			json.put("message", "");
			json.put("status", "200");
		}else {
			json.put("data",0);
			json.put("count", 0);
			json.put("message", rsp.ErrMsg);
			json.put("status", "201");
		}
    	return json;
    }
    //endregion

    //region 玩家确认提现
    @ResponseBody
    @RequestMapping(value="recharge/CashOutEnsure",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    public JSONObject CashOutEnsure(HttpServletRequest request,@RequestBody CashOutOrderSetState info) {
    	ButlerCashOutOrderSetStateRequest req = new ButlerCashOutOrderSetStateRequest();
    	ButlerCashOutOrderSetStateResponse rsp = new ButlerCashOutOrderSetStateResponse();
    	req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
    	req.OrderId = info.getOrderId();
    	req.State = info.getState();
    	req.CashOutType = info.getRechargeType();
    	req.Operator = (String)request.getSession().getAttribute("Admintruename");
    	if (req.ZoneId < 0)
			client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
		client.Execute(req, rsp);
		
		JSONObject json = new JSONObject();
		json.put("message", rsp.ErrMsg);
		json.put("code", 200);
		return json;
    }
    //endregion

    //region 跳转上分界面
    @RequestMapping("recharge/upperLowerScoreView")
    public ModelAndView upperLowerScoreView(HttpServletRequest httpServletRequest) {
    	ModelAndView mav = new ModelAndView("recharge/upperLowerScoreView");
    	return mav;
    }
    //endregion

    //region 上分操作
    @ResponseBody
    @RequestMapping(value="recharge/upperLowerScore",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    public JSONObject upperLowerScore(HttpServletRequest request,@RequestBody ButlerBankItemSendRequest BankItemSend) {
    	ButlerBankItemSendRequest req = new ButlerBankItemSendRequest();
    	ButlerBankItemSendResponse rsp = new ButlerBankItemSendResponse();
    	req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
    	req.SourceUserId = 888888;
    	req.DestUserId = BankItemSend.DestUserId;
    	req.ItemId = BankItemSend.ItemId;
    	req.ItemSubId = BankItemSend.ItemSubId;
    	req.ItemCount = BankItemSend.ItemCount;
    	if (req.ZoneId < 0)
			client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
		client.Execute(req, rsp);
		
		JSONObject json = new JSONObject();
		json.put("message", rsp.ErrMsg);
		json.put("code", 200);
		return json;
    }
    //endregion
}
